package com.que.owen.controller;

import com.que.owen.model.PageQuery;
import com.que.owen.service.TestMongoService;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName TestMongoController
 * @Author Owen.Que
 * @Description
 * @Date 8/29/2018 15:23
 **/
@RestController
@RequestMapping("/testMongo")
@Validated
public class TestMongoController {

    @Autowired
    private TestMongoService testMongoService;

    @GetMapping("/mongoOperation")
    public ResponseEntity mongoOperation() {
        testMongoService.testOperation();
        return ResponseEntity.ok("");
    }
    @GetMapping("/mongoOperation2")
    public ResponseEntity mongoOperation2() {
        testMongoService.testOperation();
        return ResponseEntity.ok("");
    }

    @GetMapping("/insertTestData/{collection}/{count}")
    public ResponseEntity insertTestData(@PathVariable @NotBlank String collection, @PathVariable Long count) {
        testMongoService.insertTestData(collection, count);
        return ResponseEntity.ok("");
    }

    @GetMapping("/paginationGet/{type}/{colName}/{colValue}/{pageSize}/{pageNo}")
    public ResponseEntity paginationGetByQuery(@PathVariable @NotBlank String type,
                                               @PathVariable @NotBlank String colName,
                                               @PathVariable Integer colValue,
                                               @PathVariable Integer pageSize,
                                               @PathVariable Integer pageNo) {
        Criteria criteria = Criteria.where(colName).is(colValue);
        Query query = Query.query(criteria);
        PageQuery pq = new PageQuery();
        pq.setPageSize(pageSize);
        pq.setCurrentPage(pageNo);
        String msg = "";
        if ("by_query".equals(type)) {
            msg = testMongoService.paginationGetByQuery(query, pq);
        } else {

            msg = testMongoService.paginationGetByAggregate(criteria, pq);
        }

        return ResponseEntity.ok(msg);
    }


    @GetMapping("/testmock/{pageNo}")
    public ResponseEntity testmock(@PathVariable Integer pageNo) {
          String  msg = testMongoService.testMock(pageNo);
        return ResponseEntity.ok(msg);
    }


}
